﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Sales;
using NotebookS2.Sales.Dialogs;

namespace NotebookS2
{
    public partial class FND_ARCM : RibbonForm
    {
        public FND_ARCM()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public string SelectedDocumentNumber { get; set; }


        public void SetSearchByDocumentNumber(string _DOCNUMBER)
        {
            txtSearch.Text = _DOCNUMBER;

            BindByDocumentNumber(txtSearch.Text);

        }


        private void txtBPCode_Properties_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {

        }
        public void BindByDocumentNumber(string _DOCUMENTNUMBER)
        {
            CreditMemoManager oManager = new CreditMemoManager();
            oManager.ConnectionString = this.ConnectionString;

            oManager.Open();

            DataTable dt = oManager.BrowseCreditMemoByDocNum(_DOCUMENTNUMBER);

            oManager.Close();

            gridControl1.DataSource = dt;
            gridControl1.Refresh();
        }

        public void BindByRemarks(string _REMARKS)
        {
            CreditMemoManager oManager = new CreditMemoManager();
            oManager.ConnectionString = this.ConnectionString;

            oManager.Open();

            DataTable dt = oManager.BrowseCreditMemoByRemarks(_REMARKS);

            oManager.Close();

            gridControl1.DataSource = dt;
            gridControl1.Refresh();
        }

        private void FND_ARCM_Load(object sender, EventArgs e)
        {
            cboSearch.SelectedIndex = 1;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtSearch_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void txtSearch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            DevExpress.XtraSplashScreen.SplashScreenManager.ShowForm(typeof(WaitForm5));

            if (cboSearch.SelectedIndex == 0)
            {
                BindByDocumentNumber(txtSearch.Text);
            }

            if (cboSearch.SelectedIndex == 1)
            {
                BindByRemarks(txtSearch.Text);
            }

            DevExpress.XtraSplashScreen.SplashScreenManager.CloseForm();
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            int iRowIndex = gridView1.FocusedRowHandle;
            this.SelectedDocumentNumber = gridView1.GetRowCellValue(iRowIndex, gridView1.Columns["DocNum"]).ToString();

            this.Close();
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

                if (cboSearch.SelectedIndex == 0)
                {
                    BindByDocumentNumber(txtSearch.Text);
                }

                if (cboSearch.SelectedIndex == 1)
                {
                    BindByRemarks(txtSearch.Text);
                }

                gridControl1.Focus();
            }
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }
        }
    }
}
